import request from '@/utils/axios/request'
import api from './api'
import getSessionContextDTO from './sessionContext'

export const apiPrefix = '/api'

const gen = (params: string) => {
  let url = apiPrefix + params
  let method = 'GET'
  const paramsArray = params.split(' ')
  if (paramsArray.length === 2) {
    method = paramsArray[0]
    url = apiPrefix + paramsArray[1]
  }

  return function (data: any) {
    // let dataObj = { ...(data || {}), ...getSessionContextDTO() }
    let dataObj = { ...(data || {}) }
    return request({
      url,
      data: dataObj,
      method
    })
  }
}

const APIFunction: any = {}
for (const key in api) {
  APIFunction[key] = gen((api as any)[key])
}

export default APIFunction
